package com.lysj.admin.file.upload.controller;

import com.lysj.admin.file.upload.domain.UploadInfo;
import com.lysj.admin.file.upload.service.UploadFileService;
import com.lysj.admin.file.upload.service.UploadInfoMd5InfoRelationService;
import com.lysj.admin.file.upload.service.UploadInfoService;
import com.lysj.admin.file.upload.vo.FileInfoVo;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author ZhangWenJian
 * @data 2019/1/4--17:12
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadInfoController extends BaseController<UploadInfo> {
    @Resource
    private UploadInfoService uploadInfoService;

    @Resource
    private UploadFileService uploadFileService;

    @Override
    public BaseService<UploadInfo> getService() {
        return uploadInfoService;
    }

    @Resource
    protected UploadInfoMd5InfoRelationService uploadInfoMd5InfoRelationService;

    @Value("${file.upload.path}")
    public String prefixPath;

    @Override
    public Resp list(UploadInfo uploadInfo, PageVo pageVo){ return null; };

    @PostMapping("/files_upload/{project}/{module}")
    public Resp filesUpload(MultipartFile[] files, String uploaderId, @PathVariable String project,@PathVariable String module){
        System.out.println("进入");
        if("undefined".equals(project) || ParamUtil.isBlank(project) ){
            return new Resp().error(Resp.Status.PARAM_ERROR,"请填写来源项目!");
        }
        if("undefined".equals(module) ||  ParamUtil.isBlank(module)){
            return new Resp().error(Resp.Status.PARAM_ERROR,"请填写来源模块!");
        }
        return uploadFileService.filesUpload(files,uploaderId,project,module);
    }

    @GetMapping("/list_rewrite")
    public Resp listRewrite(UploadInfo entity,PageVo pageVo){
        return uploadInfoService.listRewrite(entity,pageVo);
    }

    @GetMapping("/resource/{fileId}")
    public void showFiles(HttpServletRequest request, HttpServletResponse response,@PathVariable String fileId) throws IOException {
        if("undefined".equals(fileId) || ParamUtil.isBlank(fileId)){
            return;
        }
        uploadFileService.showFiles(request,response,false,fileId);
    }
    @GetMapping("/resource/thumbnail/{fileId}")
    public void showThumbnailFiles(HttpServletRequest request, HttpServletResponse response,@PathVariable String fileId) throws IOException {
        if("undefined".equals(fileId) || ParamUtil.isBlank(fileId)){
            return;
        }
        uploadFileService.showFiles(request,response,true,fileId);
    }

}
